﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Linq;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls;
using System.Collections.ObjectModel;
using System.Windows.Browser;
using System.Text;
using Baosight.Platform.Core.ServiceModel;
using Baosight.ZNH.ZMCommon.Prompt;
using System.Windows.Controls;


namespace Baosight.ZNH.ZMCT.UI
{
    public partial class ZMCT21 : EFViewCmes
    {
        private string mode;
        public ZMCT21()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
        }

        public override void Initialize(EIInfo initData)
        {
            this.fdFrequency.Value = "每日";
            mode = initData.Get("mode").ToString();
            if (mode.Equals("insert"))
            {
                this.fdModelId.Value = initData.Get("fdModelId").ToString();
            }
            base.Initialize(initData);
            base.BeforeAction += OnBeforeAction;
        }

        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.ParameterSetSP.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnConfirmButtonClick(object sender, RoutedEventArgs e)
        {
            if (ValidateResultField())
            {
                object timingTime1 = fdTimingTime1.Value;
                object timingTime2 = fdTimingTime2.Value;
                object timingTime3 = fdTimingTime3.Value;
                object timingTime4 = fdTimingTime4.Value;
                object sendingTime1 = fdSendingTime1.Value;
                object sendingTime2 = fdSendingTime2.Value;
                object sendingTime3 = fdSendingTime3.Value;
                object sendingTime4 = fdSendingTime4.Value;

                if ((!timingTime2.Equals("") && sendingTime2.Equals(""))
                    || (timingTime2.Equals("") && !sendingTime2.Equals("")))
                {
                    ValidateMessagePopup("定时生成时间2与定时发送时间2必须都填入数据！");
                    return;
                }

                if ((!timingTime3.Equals("") && sendingTime3.Equals(""))
                    || (timingTime3.Equals("") && !sendingTime3.Equals("")))
                {
                    ValidateMessagePopup("定时生成时间3与定时发送时间3必须都填入数据！");
                    return;
                }

                if ((!timingTime4.Equals("") && sendingTime4.Equals(""))
                    || (timingTime4.Equals("") && !sendingTime4.Equals("")))
                {
                    ValidateMessagePopup("定时生成时间4与定时发送时间4必须都填入数据！");
                    return;
                }

                if (timingTime2.Equals(timingTime1) 
                        || (!timingTime2.Equals("") && timingTime2.Equals(timingTime3))
                        || (!timingTime2.Equals("") && timingTime2.Equals(timingTime4))
                        || timingTime3.Equals(timingTime1)
                        || (!timingTime3.Equals("") && timingTime3.Equals(timingTime2))
                        || (!timingTime3.Equals("") && timingTime3.Equals(timingTime4))
                        || timingTime4.Equals(timingTime1)
                        || (!timingTime4.Equals("") && timingTime4.Equals(timingTime2))
                        || (!timingTime4.Equals("") && timingTime4.Equals(timingTime3)))
                {
                    ValidateMessagePopup("定时生成时间不能重复！");
                    return;
                }

                if (sendingTime2.Equals(sendingTime1)
                        || (!sendingTime2.Equals("") && sendingTime2.Equals(sendingTime3))
                        || (!sendingTime2.Equals("") && sendingTime2.Equals(sendingTime4))
                        || sendingTime3.Equals(sendingTime1)
                        || (!sendingTime3.Equals("") && sendingTime3.Equals(sendingTime2))
                        || (!sendingTime3.Equals("") && sendingTime3.Equals(sendingTime4))
                        || sendingTime4.Equals(sendingTime1)
                        || (!sendingTime4.Equals("") && sendingTime4.Equals(sendingTime2))
                        || (!sendingTime4.Equals("") && sendingTime4.Equals(sendingTime3)))
                {
                    ValidateMessagePopup("定时发送时间不能重复！");
                    return;
                }

                if (BeforeAction != null)
                {
                    BeforeAction.Invoke(null, null);
                }
                base.Submit(this.BuildSubmitEIInfo("ZMCT21", mode, "result"), DoCommitCompleted);
            }
        }

        /// <summary>
        /// 点击"确定"后的回调
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public new void DoCommitCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo inInfo = e.EIInfo;
            if (inInfo.GetStatus() == -1)
            {
                this.OnCommitMessagePopup(inInfo);//操作失败，消息提醒
            }
            else
            {
                if (mode.Equals("insert"))
                {
                    base.OpenEFWindowAlert("新增成功", true);
                }
                else
                {
                    base.OpenEFWindowAlert("修改成功", true);
                }
            }
            this.TempButtonOpen(true);
        }

        /// <summary>
        /// EFDatafield容器的全局合法性校验
        /// </summary>
        /// <param name="element">EFDatafield的容器控件</param>
        /// <returns>是否全部合法</returns>
        public bool ValidateResultField()
        {
            bool result = true;

            List<EFDataField> dataFields = this.GetChildObjects<EFDataField>(null);
            foreach (EFDataField field in dataFields)
            {
                if (field.eBindingName.Contains("result-0"))
                {
                    if (field.Visibility == Visibility.Visible)
                    {
                        result = field.Validate() && result;
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// 提示错误消息
        /// </summary>
        /// <param name="str"></param>
        private void ValidateMessagePopup(string str)
        {
            Prompts pop = Prompts.getInstance();
            pop.SetMsg(str);
            pop.Show(6);

            return;
        }
    }
}
